﻿using IR.Common.Enums;
using IR.Common.Utilities;
using IR.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IR.Controller.ModelControllers
{
    public class CVersion
    {
        private MVersion _currentVersion;

        #region **Initializers**
        /// <summary>
        /// New Version
        /// </summary>
        public CVersion()
        {
            _currentVersion = new MVersion();
        }

        /// <summary>
        /// For edit screens
        /// </summary>
        /// <param name="cUserToCopy">Push the CVersion to copy</param>
        public CVersion(CVersion cVersionToCopy)
        {
            if (cVersionToCopy != null)
            {
                _currentVersion = new MVersion();
                _currentVersion.VersionID = cVersionToCopy.VersionID;
                _currentVersion.DateCreated = cVersionToCopy.DateCreated;
                _currentVersion.Status = cVersionToCopy.Status;
            }
        }

        /// <summary>
        /// For existing Version
        /// If version doesn't exit, it will throw an exception
        /// </summary>
        /// <param name="usernameToFind">Enter the versionID to search for</param>
        public CVersion(string versionToFind)
        {
            MVersion tempVersion = MVersion.Load(versionToFind);

            if (tempVersion != null)
            {
                _currentVersion = tempVersion;
            }
            else
            {
                Exception NoVersionFound = new Exception();
                throw NoVersionFound;
            }
        }

        /// <summary>
        /// Used for Controller constructors
        /// </summary>
        /// <param name="mversionToCopy">Pass in the MVersion to copy</param>
        internal CVersion(MVersion mversionToCopy)
        {
            _currentVersion = mversionToCopy;
        }
        #endregion

        #region **Model Relations**
        public string VersionID
        {
            get
            {
                if (_currentVersion == null)
                    return string.Empty;
                return _currentVersion.VersionID;
            }
            set
            {
                if (value != null && value.Length < 11)
                    _currentVersion.VersionID = value;
                else
                    return;
            }
        }

        public DateTime DateCreated
        {
            get { return _currentVersion.DateCreated; }
            set { _currentVersion.DateCreated = System.DateTime.Now; }
        }

        public Status Status
        {
            get { return _currentVersion.Status; }
            set
            {
                if (value != null && (value != ((Status)0)))
                    _currentVersion.Status = value;
                else
                    return;
            }
        }
        #endregion

        /// <summary>
        /// Saves the current version
        /// </summary>
        /// <param name="allowOverwrite">Allow editing versions</param>
        /// <returns>True if saved</returns>
        public bool Save(bool allowOverwrite = false)
        {
            return _currentVersion.Save(allowOverwrite);
        }

        /// <summary>
        /// ***ALWAYS wrap this in a try catch... if it fails it will throw an exceiption***
        /// 
        /// Runs a query on the version table based on a Query you pass in
        /// !!!NOTE: Pass in null to get everything
        /// </summary>
        /// <param name="queries">Dictonary... key = column name, value = value in column</param>
        /// <returns>List of CVersions matching all params, all if null param</returns>
        public static List<CVersion> QueryVersions(Query queries)
        {
            // Pass in a non-null dictionary
            if (queries == null)
                queries = new Query();

            List<MVersion> modelList = MVersion.QueryVersions(queries.Queries);

            // Return an empty List if none
            if (modelList == null)
                modelList = new List<MVersion>();

            List<CVersion> returnList = new List<CVersion>(modelList.Count);

            foreach (MVersion currentVersion in modelList)
            {
                returnList.Add(new CVersion(currentVersion));
            }

            return returnList;
        }
    }
}
